﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public class ManagerWaterReadingControl
    {

        public static ICollection<WaterReadingControl> SearchByAllParameters(KardexHelper kardexToFind)
        {
            //return DataAccessWaterReadingControl.SearchWaterReadingByAllParameter(kardexToFind);
            ICollection <WaterReadingControl> resultList = DataAccessWaterReadingControl.SearchWaterReadingByParameter(kardexToFind);
            if (resultList.Count == 0)
                throw new Exception("No se encontraron Lecturas Pendientes...");

            return resultList;
        }

        public static void UpdateWaterReading(WaterReadingControl waterReading)
        {
            if (waterReading.PaidAmount == 0)
                throw new Exception("El monto no es valido");

            waterReading.ReadingState = (short)Model.ReadingState.Modificado;
            waterReading.ReadingDate = DateTime.Now.Date;
            DataAccessWaterReadingControl.UpdataWaterReadingControl(waterReading);
        }

        public static int ConfirmReadingsMade()
        {
            ICollection<WaterReadingControl> readingsMade = DataAccessWaterReadingControl.SearchWaterReadingsMade();
            if (readingsMade.Count == 0)
            {
                throw new Exception("No existen lecturas por confirmar");
            }

            foreach (WaterReadingControl waterReading in readingsMade)
            {
                waterReading.ReadingState = (short)Model.ReadingState.Confirmado;
                DataAccessWaterReadingControl.UpdataWaterReadingControl(waterReading);
                //create water collect control
                WaterCollectControl waterCollect = new WaterCollectControl()
                                                    {
                                                        CollectType = (short)WaterChargeType.Consumo_Agua,
                                                        DateGenerated = DateTime.Now.Date,
                                                        IsPaid = false,
                                                        KardexId = waterReading.KardexId,
                                                        PaymentAmount = waterReading.PaidAmount,
                                                        ReadingDate = waterReading.ReadingDate,
                                                        WaterReadingId = waterReading.WaterReadingId
                                                    };
                DataAccessWaterCollectControl.RegisterNewWaterCollectControl(waterCollect);
            }

            return readingsMade.Count;
        }

        public static void UpdateKardexAndGenerateReading(Kardex kardex, WaterReadingControl newWaterReading)
        {
            if (kardex.WaterMeasurer == string.Empty)
                throw new Exception("El numero de medidor no puede estar vacio");
            if (newWaterReading.CurrentMeasurerRead == 0)
                throw new Exception("La lectura actual no puede estar vacia");

            DataAccessKardex.UpdateKardexMeasure(kardex);

            ICollection<WaterReadingControl> readingsPast = DataAccessWaterReadingControl.GetWaterReadingActiveByKardex(kardex);
            if (readingsPast.Count > 0)
            {
                WaterReadingControl reading = readingsPast.First();
                reading.IsValidReading = false;
                DataAccessWaterReadingControl.UpdataWaterReadingControl(reading);
            }

            newWaterReading.ReadingType = (short)Model.ReadingType.Historico;
            newWaterReading.IsValidReading = true;
            newWaterReading.KardexId = kardex.KardexId;
            newWaterReading.ReadingDate = DateTime.Now.Date;
            newWaterReading.ReadingState = (short)ReadingState.Confirmado;
           
            //generate rading
            DataAccessWaterReadingControl.RegisterNewWaterReadingControl(newWaterReading);
        }

        public static decimal CalculatePaymentAmount(Model.WaterReadingControl waterReading, double currentReading)
        {
            ICollection<Model.FeesAndFines> list = DataAccessFeesAndFines.GetCurrentFeesAndFines();
            if (list.Count == 0)
            {
                throw new Exception("No existen tarifas registradas");
            }
            Model.FeesAndFines currentFees = list.First();

            decimal waterFare = waterReading.WaterFare;
            double currentRead = currentReading;
            double previousRead = waterReading.PreviousMeasurerRead;
            if (currentRead == 0)
            {
                throw new Exception("Debe llenarse la lectura actual");
            }

            decimal result = ((decimal)(currentRead - previousRead)) * waterFare;
            if (result > currentFees.BaseRate)
            {
                result = Math.Round(result, 1);
            }
            else
            {
                result = currentFees.BaseRate;
            }

            return result;
        }
    }
}
